/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.lock.core;

import java.util.concurrent.TimeUnit;

/**
 * Lock
 *
 * @author kite@iiifi.com 花朝
 */
public interface Lock {
    /**
     * 获取锁.
     *
     * @param requestId requestId
     * @param expire 过期时间
     * @param timeUnit 时间单位
     */
    void lock(String requestId, long expire, TimeUnit timeUnit) throws InterruptedException;

    /**
     * 如果锁是空闲的，则获取锁
     *
     * @param requestId requestId
     * @param expire 过期时间
     * @param timeUnit 时间单位
     * @return boolean
     */
    boolean tryLock(String requestId, long expire, TimeUnit timeUnit);

    /**
     * 尝试获取锁
     *
     * @param requestId requestId
     * @param timeout 等待时间
     * @param expire 过期时间
     * @param timeUnit 时间单位
     * @return boolean
     */
    boolean tryLock(String requestId, long timeout, long expire, TimeUnit timeUnit) throws InterruptedException;

    /**
     * 检查当前线程是否持有锁
     *
     * @return boolean
     */
    boolean isHeldByCurrentThread();

    /**
     * 是否存在锁
     *
     * @return boolean
     */
    boolean isLocked();

    /**
     * 释放锁
     */
    void unlock();
}
